package davidlauzon.activerecord.nodes.field;

import davidlauzon.activerecord.nodes.AliasableColumnIF;
import davidlauzon.activerecord.nodes.Column;
import davidlauzon.activerecord.nodes.Node;
import davidlauzon.activerecord.nodes.SortDirection;
import davidlauzon.activerecord.nodes.SortableColumnIF;
import davidlauzon.activerecord.visitor.SqlSerializer;

public class Field extends Node implements AliasableColumnIF, SortableColumnIF
{
    private String  _name;
    private boolean _isPrimaryKey = false;   // required for CREATE statement
    private boolean _isQuotable   = false;
    
    
    public String name()  { return _name; }
    public boolean isPrimaryKey() { return _isPrimaryKey; }
    
    public boolean isQuotable() { return _isQuotable; }
    protected void setQuotable( boolean quotable ) { _isQuotable = quotable; }
    
    
    public Field(String name, boolean isPrimaryKey) {
        _name         = name;
        _isPrimaryKey = isPrimaryKey; 
    }
    
    public Column as(String alias) { return new Column(this, alias, null); }
    public Column asc()  {  return new Column(this, null, SortDirection.ASC); }
    public Column desc() {  return new Column(this, null, SortDirection.DESC); }
    
    @Override public String  alias()               { return null; }
    @Override public boolean isAlias()             { return false; }
    @Override public boolean hasSortDirection()    { return false; }
    @Override public SortDirection sortDirection() { return null; }
    
    
    @Override
    public String accept(SqlSerializer visitor) {
        return visitor.visit( this );
    }
    
    public String toString() {
        return _name;
    }
}
